package hichecker.ui.custom_views.course;

import hichecker.controller.Config;
import hichecker.controller.MainController;
import hichecker.tools.export.CSVFile;
import hichecker.ui.components.custom_table.CustomTable;
import hichecker.ui.components.icon_factory.IconFactory;
import hichecker.ui.general_views.container_panel.content.ContentPanel;
import hichecker.university.Course;
import hichecker.university.UniversityPart;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;

public class CourseView extends ContentPanel {
	private MainController controller;
	private Course course;

	private CourseViewTableModel model;
	private CustomTable tblCourse;

	public CourseView(UniversityPart item, MainController controller) {
		super(item);
		this.controller = controller;
		this.course = (Course) item;

		model = new CourseViewTableModel(course);
		tblCourse = new CustomTable(model, getWidth());
		JScrollPane pane = new JScrollPane(tblCourse);


		AbstractAction deleteAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteLines();
			}
		};
		JButton btnDelete = IconFactory.getIconButton(Config.BUTTON_REMOVE, null, null, deleteAction);
		tblCourse.getInputMap().put(Config.HOTKEY_DELETE, "delete");
		tblCourse.getActionMap().put("delete", deleteAction);

		// Layout
		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
				layout.createParallelGroup().addComponent(pane).addGroup(
						layout.createSequentialGroup().addPreferredGap(
								LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addComponent(btnDelete))));

		layout.setVerticalGroup(layout.createSequentialGroup().addComponent(pane).addGroup(
				layout.createParallelGroup().addComponent(btnDelete)));

	}

	private void deleteLines() {
		// No line selected
		if (tblCourse.getSelectedRow() < 0) return;
		if (!userConfirmed()) return;

		int[] selectedRows = tblCourse.getSelectedRows();
		for (int i = selectedRows.length - 1; i >= 0; i--)
			model.deleteRow(selectedRows[i]);
	}

	@Override
	public void paint(Graphics g) {
		tblCourse.setAutoResizeable(getWidth());
		super.paint(g);
	}

	@Override
	public boolean isCorrect() {
		boolean isCorrect = tblCourse.isCorrect();
		if (isCorrect) tblCourse.setCurrentValue();
		return isCorrect;
	}

	@Override
	public ContentPanel switchViewSettings() {
		return new CourseSettings(item, controller);
	}

	@Override
	public List<CSVFile> toCSV(String topLevel) {
		String name = item.getName() + " " + course.getTerm().fitToFileName();

		List<CSVFile> files = new ArrayList<CSVFile>();
		files.add(model.toCSV(name));

		for (UniversityPart part : item.getSubParts())
			files.addAll(part.getRepresentation(false, controller).toCSV(name));

		return files;
	}
}
